<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>

<style type="text/css">
        #style-c
			{
				font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
				font-size: 12px;
				margin: 45px;
				width: 480px;
				text-align: center;
				border-collapse: collapse;
				border-top: 7px solid #9baff1;
				border-bottom: 7px solid #9baff1;
			}
			#style-c th
			{
				font-size: 13px;
				font-weight: normal;
				padding: 8px;
				background: #e8edff;
				border-right: 1px solid #9baff1;
				border-left: 1px solid #9baff1;
				color: #039;
			}
			#style-c td
			{
				padding: 8px;
				background: #e8edff; 
				border-right: 1px solid #aabcfe;
				border-left: 1px solid #aabcfe;
				color: #669;
			}

		</style>

<body>
		<%
		int pageSize = 5;//一页显示几条记录
		int rowCount;//记录总数
		int pageCount = 0;//总页数
		int pageNow;//当前页,第一次访问页面时该值为1,如果不是第一次而是点击链接则要获取链接的页码
			try {
				Class.forName("com.mysql.jdbc.Driver");
				String url = "jdbc:mysql://localhost:3306/st?useUnicode=true&characterEncoding=utf-8";
				String user = "root";
				String pass = "123456";
				Connection con = DriverManager.getConnection(url,user,pass);
				Statement  st  = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
				ResultSet rs = st.executeQuery("select * from student2");
				rs.last();//指向最后一条记录
				rowCount = rs.getRow();//获取最后一条记录的记录号
				if(rowCount%pageSize==0){
					pageCount = rowCount/pageSize;
				}else{
					pageCount = rowCount/pageSize+1;
				}
				String page_now = request.getParameter("page");
				if(page_now==null){		//如果获取的page参数为空,说明是第一次访问
					pageNow = 1;
				}else{
					pageNow = Integer.parseInt(page_now);//把String类型强转成int类型
				}
				rs.absolute((pageNow-1)*pageSize+1);	//pageNow=1 ---> rs.absolute(1)定位到第1条记录
														//pageNow=2 ---> rs.absolute(4)定位到第4条记录
														//pageNow=3 ---> rs.absolute(7)定位到第7条记录
		%>
		<table id="style-c">
			<tr>
			<td>学号</td>
			<td>系部</td>
			<td>性别</td>
			<td>年龄</td>
			<td>姓名</td>
			<td>操作</td>
			</tr>
		<%
			for(int j = 1; j <= pageSize;j++){
		%>
			<tr>
		<td><%=rs.getString("sno") %></td>
		<td><%=rs.getString("sdep") %></td>
		<td><%=rs.getString("ssex") %></td>
		<td><%=rs.getString("sage") %></td>
		<td><%=rs.getString("sname") %></td>
		<td><a href="delete.jsp?sno=<%=rs.getString("sno") %>">删除</a>&nbsp;&nbsp;<a href="insert.jsp">增加</a>&nbsp;&nbsp;
			<a href="update.jsp?sno=<%=rs.getString("sno") %>">修改</a></td>
			</tr>
		<% 
			if(rs.next()==false){
				break; //如果后面没有数据,直接退出
			}
		}
		rs.close();
		st.close();
		con.close();
		
	} catch (ClassNotFoundException e) {
		
		e.printStackTrace();
	} catch (SQLException e) {
		
		e.printStackTrace();
	}
     	%>
     <tr>
     <td colspan="6">
     <%for(int i = 1; i<=pageCount; i++) {%>
     <a href="showinfo1.jsp?page=<%=i %>">第<%=i %>页</a>
     <%} %>
     </td>
     </tr>	
		</table>
</body>
</html>